/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.plugins.usereflect;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.model.T_DB_CONFIG;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

//接口数据结果输出拦截器，生成excel
public class OUT_SAVE_EXCEL implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= "";
	public String			param_value1	= null;
	public String			param_value2	= null;
	public String			param_value3	= null;
	public String			param_value4	= null;
	public String			param_value5	= null;
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;
	
	public void init_start()
	{
		try
		{
			String ExcelFileName = Com_Fun.Get_New_GUID().replace("-","");
			SaveExcel_Template(ExcelFileName);
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
	}

	public void SaveExcel_Template(String ExcelFileName)
	{
		JSONObject jso = null;
		WritableWorkbook book = null;
		JSONArray jsary = null;
		try
		{
			jso = new JSONObject(this.strResult.toString());
			
			// 创建一个Excel文件对象
			String strTFileName = Com_Para.UpLoad_File + Com_Para.zxyPath
					+ "temExcel" + Com_Para.zxyPath + ExcelFileName + ".xls";
			
			File file = new File(Com_Para.UpLoad_File + Com_Para.zxyPath
					+ "temExcel" + Com_Para.zxyPath);
			if(!file.exists() && !file.isDirectory())
			{
				file.mkdir();
			}
			
			file = new File(strTFileName);
			book = Workbook.createWorkbook(file);
			// 创建Excel第一个选项卡对象
			WritableSheet sheet = book.createSheet("sheet",0);
			jsary = jso.getJSONArray(this.param_name);
			
			List<String> aryTitle = new ArrayList<String>();
			if(jsary.length() > 0)
			{
				Iterator<String> titleTem = jsary.getJSONObject(0).keys();
				int iColumn = 0;
				while(titleTem.hasNext())
				{
					String strT = titleTem.next();
					aryTitle.add(strT);
					Label label1 = new Label(iColumn,0,strT);// 对应为第1列第1行的数据
					sheet.addCell(label1);
					iColumn ++;
				}
			}
			// 遍历集合并添加数据到行，每行对应一个对象
			for(int i = 0;i < jsary.length();i++)
			{
				JSONObject jsobtem = jsary.getJSONObject(i);
				for(int j = 0; j < aryTitle.size(); j ++)
				{
					sheet.addCell(new Label(j,i + 1,jsobtem.getString(aryTitle.get(j))));
				}
			}
			// 写入数据到目标文件
			book.write();
			
			JSONObject jsoFile = new JSONObject(new LinkedHashMap());
			jsoFile.put("s_result","1");
			jsoFile.put("error_desc","数据Excel生成成功");
			jsoFile.put("file_guid",ExcelFileName + ".xls");
			
			JSONArray jsaryFile = new JSONArray();
			jsaryFile.put(jsoFile);
			jso.put("OUT_SAVE_EXCEL",jsaryFile);
			this.strResult.delete(0,this.strResult.length());
			this.strResult.append(jso.toString());
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			try
			{
				// 关闭
				book.close();
			}
			catch(Exception e)
			{
			}
		}
	}
	
	public StringBuilder getStrResult()
	{
		return strResult;
	}

	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}

	public String getUser_main_id()
	{
		return user_main_id;
	}

	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}

	public String getSession_id()
	{
		return session_id;
	}

	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}

	public String getParam_name()
	{
		return param_name;
	}

	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}

	public String getParam_value1()
	{
		return param_value1;
	}

	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}

	public String getParam_value2()
	{
		return param_value2;
	}

	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}

	public String getParam_value3()
	{
		return param_value3;
	}

	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}

	public String getParam_value4()
	{
		return param_value4;
	}

	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}

	public String getParam_value5()
	{
		return param_value5;
	}

	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}

	public String getParam_value6()
	{
		return param_value6;
	}

	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}

	public String getParam_value7()
	{
		return param_value7;
	}

	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}

	public String getParam_value8()
	{
		return param_value8;
	}

	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}

	public String getParam_value9()
	{
		return param_value9;
	}

	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}

	public String getParam_value10()
	{
		return param_value10;
	}

	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}
